package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.dialog.CookProgressDialog;
import com.lanou.dllo.findfood.entity.RestRunt_json;
import com.lanou.dllo.findfood.entity.RestaurantSencond;
import com.lanou.dllo.findfood.tools.TimeUtil;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/10/27.
 */
public class HomePagelastActivity extends Activity {
    private ImageView page_image;
    private WebView PagewebView, titlewebView;
    private TextView page_title, long_title, sale_number, market_price,
            current_price, min_title, page_consumer_tips;
    private int deal_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage_last);
        initView();
        getTitltData();
        initData();


    }

    private void initView() {
        page_image = (ImageView) findViewById(R.id.page_image);
        page_title = (TextView) findViewById(R.id.page_title);
        long_title = (TextView) findViewById(R.id.long_title);
        sale_number = (TextView) findViewById(R.id.sale_number);
        market_price = (TextView) findViewById(R.id.market_price);
        market_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        current_price = (TextView) findViewById(R.id.current_price);
        min_title = (TextView) findViewById(R.id.min_title);
        PagewebView = (WebView) findViewById(R.id.PagewebView);
    }

    private void getTitltData() {

        deal_id = getIntent().getIntExtra("deal_id", 2327781);

    }

    private void initData() {
        final CookProgressDialog anmition = new CookProgressDialog(this, R.id.animaImg);
        anmition.show();
        String httpUrl = "http://apis.baidu.com/baidunuomi/openapi/dealdetail" + "?" + "deal_id=" + deal_id;

        StringRequest request = new StringRequest(httpUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();

                RestRunt_json result = gson.fromJson(response, RestRunt_json.class);
                if (result != null) {
                    RestaurantSencond r = result.getDeal();
                    if (r != null) {


                        page_title.setText(r.getTitle());
                        //    page_consumer_tips.setText(r.getConsumer_tips());

                        //  page_consumer_tips.setText(r.getBuy_details());
                        final String mimrTypeString = "text/html";
                        final String encoding = "utf-8";
                        final String htmlString = r.getConsumer_tips()+r.getBuy_details();
                        PagewebView.loadDataWithBaseURL("fake://not/needed",
                                htmlString, mimrTypeString, encoding, "");

                        // 设置可以支持缩放
                        PagewebView.getSettings().setSupportZoom(true);
                        PagewebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
                        PagewebView.getSettings().setLoadWithOverviewMode(true);//设置webview自适应屏蔽
                     //   PagewebView.setInitialScale(120);
                        current_price.setText(r.getCurrent_price());
                        market_price.setText(r.getMarket_price());
                        sale_number.setText(r.getSale_num());
                        long_title.setText(r.getLong_title());

                       // min_title.setText(r);
//                        long t = r.getUpdate_time();
//                      //  min_title.setText(TimeUtil.getFormatTimeFromTimestamp(t,"yyyy-MM-dd HH:MM"));
//                        SimpleDateFormat sf = null;
//
//                        Date d = new Date(t);
//                        sf = new SimpleDateFormat("yyyy年MM月dd日");
//                        min_title.setText(sf.format(d));

                        String img = r.getImage();
                        if (img != null) {
                            getUser_img(img, page_image);
                        }
                        anmition.dismiss();
                    }


                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();

            }
        }) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(request);
    }

    public void getUser_img(String user_img, final ImageView imageView) {
        ImageRequest request = new ImageRequest(user_img,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {
                        imageView.setImageBitmap(response);


                    }
                }, 300, 300, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        VolleySington.getInstance().addRequest(request);


    }


}
